//
//  WatchViewController.m
//  BaseProject
//
//  Created by hundredsandc on 2017/10/25.
//  Copyright © 2017年 hundredsandc. All rights reserved.
//

#import "WatchViewController.h"
#import "LiveTableViewCell.h"
#import "LiveListViewController.h"
#import "HTTPManagers.h"
#import "LiveModel.h"
#import "MJExtension.h"
#import "WEBViewController.h"




@interface WatchViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UISegmentedControl *segment;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArr;
@end

@implementation WatchViewController
{
    NSInteger page;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    page = 0;
//    [self getData];
    [self getBackLoooksDatas];

//    self.navigationItem.titleView = self.segment;
    [self.view addSubview:self.tableView];
    
}

#pragma mark - tableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    LiveModel *model = self.dataArr[indexPath.row];
    if (model.backLooks.count>0) {
        NSInteger i = indexPath.row;
        LiveListViewController *vc = [LiveListViewController new];
        vc.backLooksArr = model.backLooks;
        vc.timeString = model.timeString;
        [self.navigationController pushViewController:vc animated:YES];
        NSLog(@"点击了%ld行",i);

    }
//    else if(model.sinaMatchDataUrl.length>0){
//        WEBViewController *web = [[WEBViewController alloc]init];
//        web.url = model.sinaMatchDataUrl;
//        [self.navigationController pushViewController:web animated:YES];
//        
//    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LiveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LiveTableViewCell class]) forIndexPath:indexPath];
    
        LiveModel *model = (LiveModel *)_dataArr[indexPath.row];
        [cell customTeamOneIconUrl:model.homeTeamLogo teamOneTitle:model.homeTeamName teamOneScore:model.homeScore teamTwoIconUrl:model.awayTeamLogo teamTwoTitle:model.awayTeamName teamTwoScore:model.awayScore typeName:model.leagueName time:model.timeString];

    return cell;
    
}

-(void)getData{
    NSString *timeSp = [self getNowTimeTimestamp3];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"leagueId"]=@"0";
    params[@"page"] = @(page);
    params[@"pageSize"] = @"20";
    params[@"version"]= @"204";
    params[@"time"] = timeSp;
    params[@"sign"]=@"4439065ef4268a60f3e9c8b8d5e1506c";
    params[@"channel"]=@"AppStore";
    params[@"os_ver"] = @"10.2.1";
    params[@"platform"] = @"ios";
    
    [HTTPManagers getWithLiveURL:@"game_progress_list" params:params success:^(id json) {
        NSArray *arr= [json objectForKey:@"data"];

        for (int i = 0; i<arr.count; i++) {
            NSDictionary *dict = arr[i];
            LiveModel *model = [LiveModel mj_objectWithKeyValues:dict];
            [self.dataArr addObject:model];
            
        }
        [_tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];

}
-(void)getBackLoooksDatas{
    NSString *timeSp = [self getNowTimeTimestamp3];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"leagueId"]=@"0";
    params[@"page"] = @(page);
    params[@"pageSize"] = @"20";
    params[@"version"]= @"204";
    params[@"time"] = timeSp;
    params[@"sign"]=@"500ff05f4d877fbf9a14689ceac4c5eb";
    params[@"channel"]=@"AppStore";
    params[@"os_ver"] = @"10.2.1";
    params[@"platform"] = @"ios";
    
    [HTTPManagers getWithLiveURL:@"game_result_list" params:params success:^(id json) {
        NSArray *arr= [json objectForKey:@"data"];
        if (self.dataArr.count>0) {
            [_dataArr removeAllObjects];
        }
        for (int i = 0; i<arr.count; i++) {
            NSDictionary *dict = arr[i];
            LiveModel *model = [LiveModel mj_objectWithKeyValues:dict];
            [self.dataArr addObject:model];
            
        }
        [_tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}
-(void)changeSegment:(UISegmentedControl *)Seg{
    NSInteger Index = Seg.selectedSegmentIndex;
    page = 0;
    switch (Index) {
        case 0:
            NSLog(@"直播");
            [self changeLive:Index];
            
            break;
        case 1:
            NSLog(@"录播");
            [self changeLive:Index];
            
            break;
        default:
            break;
    }
}

-(void)changeLive:(NSInteger)index{
    if (index==0) {
    //直播
        [self getData];
    }else{
    //录播
        [self getBackLoooksDatas];
    }
}
#pragma mark property
-(UITableView *)tableView{
    if (!_tableView) {
        WS(ws);
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-FTab) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LiveTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LiveTableViewCell class])];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            if (_segment.selectedSegmentIndex==0) {
                [ws getData];
            }else{
                [ws getBackLoooksDatas];
            }
            [_tableView.mj_header endRefreshing];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            page++;
            if (_segment.selectedSegmentIndex==0) {
                [ws getData];
            }else{
                [ws getBackLoooksDatas];
            }
            [_tableView.mj_footer endRefreshing];
        }];
    }
    return _tableView;
}
-(UISegmentedControl *)segment{
    if (!_segment) {
        _segment = [[UISegmentedControl alloc]initWithItems:@[@"直播",@"录播"]];
        UIFont *font = [UIFont boldSystemFontOfSize:16.0f];
        [_segment setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:font} forState:UIControlStateSelected];
        [_segment setTitleTextAttributes:@{NSFontAttributeName:font} forState:UIControlStateNormal];
        _segment.size = CGSizeMake(154, 28);
        _segment.selectedSegmentIndex = 0;
        _segment.tintColor = RGBCOLOR(0x25, 0x43, 0x7f);
        [_segment addTarget:self action:@selector(changeSegment:) forControlEvents:UIControlEventValueChanged];
    }
    return _segment;
}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}


@end
